from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from utils.send_code_task import send_code
import redis
# Create your views here.




class Smscode(APIView):
    def get(self,request):
        mobile=request.query_params.get("mobile")
        print('mobile>>>>>>>',mobile)
        resp=send_code.delay(mobile)
        return Response({"code":200,'msg':'ok'})

class Register(APIView):
    def post(self,reqeust):
        mobile=reqeust.data.get('mobile')
        smscode=reqeust.data.get('smscode')
        password=reqeust.data.get('password')
        passcheck=reqeust.data.get('passcheck')
        if not all([mobile,password,passcheck]):
            return Response({'code':400,'msg':"数据不完整"})
        cls=redis.Redis(host='127.0.0.1',port=6379,db=5)
        cls_data=cls.get(mobile)
        if cls_data.decode() != smscode:
            return Response({'code':401,'msg':"验证码错误"})
        try:
            user=User.objects.get(mobile=mobile)
            return Response({'code':402,'msg':"手机号重复"})
        except Exception as e:
            User.objects.create(
                mobile=mobile,
                password=password
            )
            return Response({'code':200,'msg':"ok"})

class Login(APIView):
    def post(self,request):
        mobile=request.data.get('mobile')
        password=request.data.get('password')
        if not all([mobile,password]):
            return Response({'code':400,'msg':"数据不完整"})
        try:
            user=User.objects.get(mobile=mobile)
            if user.password != password:
                return Response({'code':401,'msg':"密码错误"})
        except Exception as e:
            print(e)
            return Response({'code':200,'msg':"手机号不存在"})
        return Response({'code':200,'msg':'ok',"id":user.id})



class LeiView(APIView):
    def get(self,request,id):
        lei_data=LeiBie.objects.filter(user_id=id)
        lei_list=[]
        for i in lei_data:
            lei_list.append({
                'id':i.id,
                'img':i.img,
                'name':i.name,
                'user':i.user.mobile
            })
        return Response({'code':200,'msg':lei_list})

class ShangView(APIView):
    def get(self,request,id):
        lei_data = ShangPin.objects.filter(lei_id=id)
        lei_list = []
        for i in lei_data:
            lei_list.append({
                'id': i.id,
                'img': i.img,
                'name': i.name,
                'num':i.num,
                'price':i.price,
                'lei': i.lei.name
            })
        return Response({'code': 200, 'msg': lei_list})

class Xiu(APIView):
    def put(self, request):
        mobile = request.data.get("mobile")
        password= request.data.get("password")
        pwd=request.data.get("pwd")
        try:
            user=User.objects.get(mobile=mobile)
        except Exception:
            return Response({'code': 200, 'msg': "手机号不存在"})
        if user.password != password:
                return Response({'code':401,'msg':"密码错误"})
        else:
                User.objects.filter().update(password=pwd)
                return Response({"code": 200, "msg":"OKK"})











